package com.keepers.repair.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.keepers.repair.R;
import com.keepers.repair.helper.IntentHelper;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.helper.bus.LoginExitEvent;
import com.keepers.repair.helper.bus.RxBus;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.utils.DefaultUtils;
import com.keepers.repair.utils.DialogUtil;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/10
 */
public class AccountSettingActivity extends BaseActivity {

    private UserModel loginUserInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_setting);
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader("账户信息");
        TextView userName = f(R.id.user_name);
        loginUserInfo = SpHelper.getLoginUserInfo();
        userName.setText(loginUserInfo.UserName);
        TextView name = f(R.id.name);
        name.setText(loginUserInfo.Name);
        TextView group = f(R.id.group);
        group.setText(loginUserInfo.GroupName);
        f(R.id.password).setOnClickListener(v -> IntentHelper.goPasswordSetting(this));
        f(R.id.exit).setOnClickListener(v ->
                DialogUtil.showCommonDialog(this, "提示", "确定要退出吗", (dialog, which) -> exit(), null)
        );
    }

    private void exit() {
        rxHelper.pend(DataLoader.logout(loginUserInfo.Gid, aBoolean -> {
            if (DefaultUtils.value(aBoolean)) {
                SpHelper.setLoginUserInfo(null);
                RxBus.getDefault().post(new LoginExitEvent());
                ActivityUtils.finishAllActivities();
                IntentHelper.goLogin(this);


            } else {
                AlertToast.show("请稍后尝试...");
            }
        }));
    }
}
